import os
import datetime as dt
from sys import argv


path = argv[1]

print("time: ", dt.datetime.now().strftime("%Y/%m/%d %H:%M:%S"))
cmd_ps = "ps -ax | grep " + path
pid = ""
lines = os.popen(cmd_ps)
for line in lines.readlines():
    if "python3 " + path in line:
        pid = line.split(" ")[0]


if pid != "":
    print("found pid: ", pid)
    cmd_kill = "kill -9 " + pid
    os.system(cmd_kill)
    print("killed: " + pid)
else:
    print("pid not found")

cmd_start = "nohup python3 " + path + " > nohup.log 2>&1 &"
os.system(cmd_start)

pid = ""
lines = os.popen(cmd_ps)
for line in lines.readlines():
    if "python3 " + path in line:
        pid = line.split(" ")[0]

if pid != "":
    print("started success at pid: " + pid)
else:
    print("started failed")


